import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.InjectionConfig;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class CodeGenerator {
    public static void main(String[] args) {
        AutoGenerator generator = new AutoGenerator();
        generator.setDataSource(getDataSource());
        generator.setGlobalConfig(getGlobalConfig());
        generator.setPackageInfo(getPackageConfig());
        generator.setCfg(getInjectionConfig());
        generator.setTemplate(getTemplateConfig());
        generator.setStrategy(getStrategyConfig());
        generator.setTemplateEngine(new FreemarkerTemplateEngine());
        generator.execute();
    }

    private static DataSourceConfig getDataSource() {
        String driver = "com.mysql.jdbc.Driver";
        String url = "jdbc:mysql://mysql.firebug.org:3306/test?useUnicode=true&characterEncoding=utf8&useSSL=false";
        String username = "writer";
        String password = "123456";
        DataSourceConfig config = new DataSourceConfig();
        config.setDriverName(driver);
        config.setUrl(url);
        config.setUsername(username);
        config.setPassword(password);
        return config;
    }

    private static GlobalConfig getGlobalConfig() {
        String author = "Felix";
        String outputDir = System.getProperty("user.dir") + "/src/main/java";
        GlobalConfig config = new GlobalConfig();
        config.setActiveRecord(false);
        config.setAuthor(author);
        config.setBaseColumnList(false);
        config.setBaseResultMap(false);
        config.setDateType(DateType.SQL_PACK);
        config.setEnableCache(false);
        config.setFileOverride(true);
        config.setIdType(IdType.ASSIGN_ID);
        config.setOpen(false);
        config.setOutputDir(outputDir);
        config.setSwagger2(true);
//        config.setControllerName("");
//        config.setServiceName("");
//        config.setServiceImplName("");
//        config.setMapperName("");
//        config.setEntityName("");
//        config.setXmlName("");
        return config;
    }

    private static PackageConfig getPackageConfig() {
        PackageConfig config = new PackageConfig();
        config.setParent("org.firebug");
//        config.setModuleName(scanner("模块A.模块B"));
        config.setModuleName("first");
        config.setController("controller.api");
        config.setService("service");
        config.setServiceImpl("service.impl");
        config.setEntity("repository.entity");
        config.setMapper("repository.mapper");
        config.setXml("mapper.xml");
        config.setPathInfo(null);
        return config;
    }

    private static InjectionConfig getInjectionConfig() {
        InjectionConfig cfg = new InjectionConfig() {
            @Override
            public void initMap() {
            }
        };
        List<FileOutConfig> fileOutConfigs = new ArrayList<>();
//        focList.add(new FileOutConfig(templatePath) {
//            @Override
//            public String outputFile(TableInfo tableInfo) {
//                return null;
//            }
//        });
        cfg.setFileOutConfigList(fileOutConfigs);
        return cfg;
    }

    private static TemplateConfig getTemplateConfig() {
        TemplateConfig config = new TemplateConfig();
        config.setEntity("/templates/entity.java");
        config.setEntityKt(null);
        config.setMapper("/templates/mapper.java");
        config.setService("/templates/service.java");
        config.setServiceImpl("/templates/serviceImpl.java");
        config.setController("/templates/controller.java");
        config.setXml(null);
        return config;
    }

    private static StrategyConfig getStrategyConfig() {
        StrategyConfig strategy = new StrategyConfig();
//        common
        strategy.setNaming(NamingStrategy.underline_to_camel);
        strategy.setColumnNaming(NamingStrategy.underline_to_camel);

//        database
        strategy.setSkipView(true);
        strategy.setCapitalMode(false);
        strategy.setTablePrefix("t_");
//        strategy.setInclude(scanner("表名A,表名B").split(","));
        strategy.setInclude("t_user");
        strategy.setExclude(null);
//        strategy.setLikeTable(new LikeTable("t_", SqlLike.RIGHT));
//        strategy.setNotLikeTable(new LikeTable("t_", SqlLike.RIGHT));
//        strategy.setNameConvert(null);
//        strategy.setTableFillList(null);

//        super class
//        strategy.setSuperControllerClass("");
//        strategy.setSuperServiceClass("");
//        strategy.setSuperServiceImplClass("");
//        strategy.setSuperMapperClass("");
//        strategy.setSuperEntityClass("");
//        strategy.setSuperEntityColumns("id");

//        controller
        strategy.setRestControllerStyle(true);
        strategy.setControllerMappingHyphenStyle(true);

//        entity
        strategy.setEntityBooleanColumnRemoveIsPrefix(false);
        strategy.setEntityBuilderModel(true);
        strategy.setEntityColumnConstant(false);
        strategy.setEntityLombokModel(true);
        strategy.setEntitySerialVersionUID(true);
        strategy.setEntityTableFieldAnnotationEnable(true);

//        entity.field
        strategy.setFieldPrefix("");
        strategy.setVersionFieldName("version"); // 必填:""
        strategy.setLogicDeleteFieldName("deleted"); // 必填:""
        return strategy;
    }

    private static String scanner(String info) {
        System.out.println("请输入 " + info + ": ");
        Scanner scanner = new Scanner(System.in);
        if (scanner.hasNext()) {
            return scanner.next();
        }
        throw new RuntimeException("输入错误");
    }
}
